package com.chookss.base

import android.content.Context
import com.chookss.MyApp
import com.johnrambo.ktea.common.launchUI
import com.johnrambo.ktea.net.http.RequestData

/**
 * token管理类
 * Create by JohnRambo at 2018/11/28
 */
object Token {
    private const val NAME = "token"
    private const val CITY = "city"
    private const val VERSIONNAME = "versionName"

    private val SP by lazy {
        MyApp.appCtx.getSharedPreferences(NAME, Context.MODE_PRIVATE)
    }

    /**
     * 清空token
     */
    fun quit() {
        RequestData.token = ""
        launchUI {
            val editor = SP.edit().remove(NAME)
            editor.apply()
            editor.commit()
        }
    }

    /**
     * 获取和设置token
     */
    var token: String
        get() = SP.getString(NAME, "").toString()
        set(value) {
            RequestData.token = value
            launchUI {
                val editor = SP.edit().apply {
                    remove(NAME)
                    putString(NAME, value)
                }
                editor.apply()
                editor.commit()
            }
        }
    /**
     * 获取版本号
     */
    var versionName: String
        get() = SP.getString(VERSIONNAME, "").toString()
        set(value) {
            RequestData.versionName = value
            launchUI {
                val editor = SP.edit().apply {
                    remove(VERSIONNAME)
                    putString(VERSIONNAME, value)
                }
                editor.apply()
                editor.commit()
            }
        }
    /**
     * 获取和设置城市
     */
    var city: String
        get() = SP.getString(CITY, "").toString()
        set(value) {
            launchUI {
                val editor = SP.edit().apply {
                    remove(CITY)
                    putString(CITY, value)
                }
                editor.apply()
                editor.commit()
            }
        }
}